
package keeto.com.vn.classified.adapter; 

import java.util.ArrayList;

import com.androidquery.AQuery;

import keeto.com.vn.classified.network.entity.CommentEntity;
import keeto.com.vn.classified.ui.R;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;

public class CommentViewAdapter extends BaseAdapter{

	private ArrayList<CommentEntity> array; 
	private Context context;
	private LayoutInflater mInflater;
	private AQuery mQuery;

	
	
	public CommentViewAdapter(ArrayList<CommentEntity> mArray, Context mcontext, AQuery query) {
		this.array = mArray;
		this.context = mcontext;
		this.mInflater = LayoutInflater.from(mcontext);
		this.mQuery = query;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		if (array == null || array.size() == 0){
			return 0;
		}else {
			return array.size();
		}
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return array.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		ViewHolder holder = null;
		if (convertView == null){
			convertView = mInflater.inflate(R.layout.comment_shop_view, parent, false);
			holder = new ViewHolder();
			holder.img_avatar = (ImageView)convertView.findViewById(R.id.img_avatar_comment);
			holder.tv_name = (TextView)convertView.findViewById(R.id.name_commentor);
			holder.ratingBar = (RatingBar)convertView.findViewById(R.id.rating_product);
			holder.tv_time = (TextView)convertView.findViewById(R.id.num_rate_product1);
			holder.tv_titile_comment=(TextView)convertView.findViewById(R.id.tv_tittle_comment);
			holder.tv_content_comment=(TextView)convertView.findViewById(R.id.tv_content_comment);
			convertView.setTag(holder);
		}else{
			holder= (ViewHolder) convertView.getTag();
		}
		CommentEntity entity = array.get(position);
		String avatar_url = entity.getUserAvatar();
		if(avatar_url != null && avatar_url.length() > 0){
			mQuery.id(holder.img_avatar).image(avatar_url, true, true, 0, 0, null, 0, 1.0f);
		} else {
			mQuery.id(holder.img_avatar).image(R.drawable.no_image);

		}
		holder.tv_name.setText(entity.getUserCommentName());
		holder.ratingBar.setRating(entity.getRating());
		holder.tv_time.setText(entity.getCommentDate());
		holder.tv_titile_comment.setText(entity.getCommentTitle());
		holder.tv_content_comment.setText(entity.getCommentContent());
		return convertView;
	} 
	
	class ViewHolder{
		private ImageView img_avatar;
		private TextView tv_name;
		private RatingBar ratingBar;
		private TextView tv_time;
		private TextView tv_titile_comment;
		private TextView tv_content_comment;
	}
}